/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.admin.bulletin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.admin.bulletin.BulletinReadRecord;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.admin.bulletin.BulletinReadRecord;
import com.fuyao.cloud.admin.bulletin.service.BulletinReadrecordService;
import com.fuyao.cloud.common.security.service.CloudUser;
import com.fuyao.cloud.common.security.util.SecurityUtils;
import liquibase.pro.packaged.B;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 公告浏览记录表
 *
 * @author lyhills
 * @date 2023-06-29 17:39:32
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/bulletinreadrecord" )
@Api(value = "bulletinreadrecord", tags = "公告浏览记录表 管理")
public class BulletinReadrecordController {

    private final  BulletinReadrecordService bulletinReadrecordService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param bulletinReadrecord 公告浏览记录表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('bulletin_bulletinreadrecord_view')" )
    public R getBulletinReadrecordPage(Page page, BulletinReadRecord bulletinReadrecord) {
        return R.ok(bulletinReadrecordService.page(page, Wrappers.query(bulletinReadrecord)));
    }


    /**
     * 通过id查询公告浏览记录表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('bulletin_bulletinreadrecord_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(bulletinReadrecordService.getById(id));
    }

    /**
     * 新增公告浏览记录表
     * @param bulletinReadrecord 公告浏览记录表
     * @return R
     */
    @ApiOperation(value = "新增公告浏览记录表 ", notes = "新增公告浏览记录表 ")
    @SysLog("新增公告浏览记录表 " )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bulletin_bulletinreadrecord_add')" )
    public R save(@RequestBody BulletinReadRecord bulletinReadrecord) {
        return R.ok(bulletinReadrecordService.save(bulletinReadrecord));
    }

    /**
     * 修改公告浏览记录表
     * @param bulletinReadrecord 公告浏览记录表
     * @return R
     */
    @ApiOperation(value = "修改公告浏览记录表 ", notes = "修改公告浏览记录表 ")
    @SysLog("修改公告浏览记录表 " )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bulletin_bulletinreadrecord_edit')" )
    public R updateById(@RequestBody BulletinReadRecord bulletinReadrecord) {
        return R.ok(bulletinReadrecordService.updateById(bulletinReadrecord));
    }

    /**
     * 通过id删除公告浏览记录表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除公告浏览记录表 ", notes = "通过id删除公告浏览记录表 ")
    @SysLog("通过id删除公告浏览记录表 " )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('bulletin_bulletinreadrecord_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(bulletinReadrecordService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param bulletinReadrecord 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bulletin_bulletinreadrecord_export')" )
    public List<BulletinReadRecord> export(BulletinReadRecord bulletinReadrecord) {
        return bulletinReadrecordService.list(Wrappers.query(bulletinReadrecord));
    }

    /**
     * 添加浏览记录
     *
     * @param bulletinId 公告ID
     * @return R
     */
    @PostMapping("/addBrowseRecord/{bulletinId}")
    public R addBrowse(@PathVariable("bulletinId") Long bulletinId) {
        BulletinReadRecord bulletinReadrecord = new BulletinReadRecord();
        bulletinReadrecord.setUnid(UUID.randomUUID().toString());
        bulletinReadrecord.setBulletinId(bulletinId);
        CloudUser user = SecurityUtils.getUser();
        bulletinReadrecord.setReadTime(LocalDateTime.now());
        bulletinReadrecord.setReaderId(user.getId());
        bulletinReadrecord.setReaderName(user.getName());
        boolean save = this.bulletinReadrecordService.save(bulletinReadrecord);
        return R.ok(save,"success");
    }
}
